package model;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;
import databeans.DroidChatUserBean;

public class DroidChatUserDAO {
	// the BeanFactory
	private BeanFactory<DroidChatUserBean> factory;
	
	public DroidChatUserDAO() throws DAOException{
		BeanTable<DroidChatUserBean> userTable = BeanTable.getInstance(DroidChatUserBean.class,"DroidChat_user");
		
		if (!userTable.exists()) {
			userTable.create("userID");   // userID is the primary key (auto-increment)
		}
		
		factory = userTable.getFactory();
	}
	
	/****
	 * This create() method that creates a new user record in the "DroidChat_user"
	 *  table given a DroidChatUserBean containing all the fields that describe a
	 *  DroidChatUserBean, except for the user id, which is generated by the server.
	 *  
	 * It returns a new DroidChatUserBean that contains a copy of the information
	 *  given to the create() method and contains the server generated user id.
	 *  
	 * @param newUser
	 * @return
	 * @throws DAOException
	 */
	public DroidChatUserBean create(DroidChatUserBean newUser) throws DAOException {
		DroidChatUserBean dbNewUser;
		
		try {
			if(factory.match(MatchArg.equalsIgnoreCase("username", newUser.getUsername())).length!=0)
				throw new DuplicateKeyException("username has already been used: " + newUser.getUsername());
			
			Transaction.begin();
			dbNewUser = factory.create();
			dbNewUser.setUsername(newUser.getUsername());
			dbNewUser.setPassword(newUser.getPassword());
			dbNewUser.setBytes(newUser.getBytes());
			dbNewUser.setAvailability(0);
			dbNewUser.setBG(newUser.getBG());
			dbNewUser.setNickname(newUser.getNickname());
			//factory.copyInto(newUser, dbNewUser);
			Transaction.commit();
			return dbNewUser;
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
	}
	
	/****
	 * This lookup() method takes a user's username and returns a DroidChatUserBean
	 *  containing information about that user.
	 *  
	 *  If no user in the db has this username, null is returned.
	 *  
	 * @param username
	 * @return
	 * @throws DAOException
	 */
	public DroidChatUserBean lookup(String username) throws DAOException{
		DroidChatUserBean[] arr=null;
		
		try {
			arr = factory.match(MatchArg.equalsIgnoreCase("username", username));
		} catch (RollbackException e) {
			e.printStackTrace();
		}
		
		if(arr==null){
			return null;
		}else if(arr.length==0){
			return null;
		}
		return arr[0];
	}
	
	/****
	 * This lookupByID() method takes a user's userID (primary key) and
	 *  returns a UserBean containing information about that user.
	 *  
	 *  If no user in the db has this userID, null is returned.
	 *  
	 * @param userID
	 * @return
	 * @throws DAOException
	 */
	public DroidChatUserBean lookupByID(int userID) throws DAOException{
		DroidChatUserBean[] arr=null;
		try {
			arr = factory.match(MatchArg.equals("userID", userID));
		} catch (RollbackException e) {
			e.printStackTrace();
		}
		if(arr==null){
			return null;
		}else if(arr.length==0){
			return null;
		}
		return arr[0];
	}
}
